package day05;


import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.context.support.GenericApplicationContext;

import javax.annotation.PostConstruct;


/**
 * @title: App64_2
 * @author: lbj
 * @date: 2022/5/2 8:42 上午
 * @description: 演示 3 级缓存，解决 setter 注入循环依赖问题
 */


public class App60 {

    static class A{
        private static final Logger log = LoggerFactory.getLogger("A");
        private B b;

        public A(){
            log.debug("A()");
        }

        @Autowired
        public void setB(B b){
            log.debug("setB({})",b.getClass());
            this.b=b;
        }

        @PostConstruct
        public void init(){
            log.debug("a init()");
        }

        public void foo(){
            System.out.println("foo()");
        }
    }

    static class B{
        private static final Logger log = LoggerFactory.getLogger("B");
        private A a;

        public B(){
            log.debug("B()");
        }

        @Autowired
        public void setB(A a){
            log.debug("setA({})",a.getClass());
            this.a = a;
        }

        @PostConstruct
        public void init(){
            log.debug("b init()");
        }
    }

    @Aspect
    static class MyAspect{
        @Before("execution(* foo())")
        public void before(){
            System.out.println("before...");
        }
    }

    public static void main(String[] args) {
        final GenericApplicationContext context = new GenericApplicationContext();
        context.registerBean("a",A.class);
        context.registerBean("b", B.class);

        //测试代理在 3 级缓存中的使用
        context.registerBean(MyAspect.class);
        context.registerBean(AnnotationAwareAspectJAutoProxyCreator.class);
        AnnotationConfigUtils.registerAnnotationConfigProcessors(context.getDefaultListableBeanFactory());
        context.refresh();

        context.getBean(A.class).foo();
    }

}
